package tk.kolesnikov.algorithms;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by seko0313 on 16.01.14.
 */
public class ImageUtils {

    public BufferedImage graysсale(BufferedImage image) {
        BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = result.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return result;
    }

    //TODO: slow implementation
    public BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return result;
    }

    /**
     * @param image
     * @return array of average alpha and rgb colors of image [av_alpha, av_red, av_green, av_blue]
     */
    public int[] getAverageColors(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        int rgb;
        int alpha = 0;
        int red = 0;
        int green = 0;
        int blue = 0;
        int[] result = new int[4];

        for (int x = 0; x < w; x++)
            for (int y = 0; y < h; y++) {
                rgb = image.getRGB(x, y);
                alpha += (rgb >> 24) & 0xff;
                red += (rgb >> 16) & 0xff;
                green += (rgb >> 8) & 0xff;
                blue += (rgb) & 0xff;
            }

        result[0] = alpha / (h * w);
        result[1] = red / (h * w);
        result[2] = green / (h * w);
        result[3] = blue / (h * w);

        return result;
    }

    /**
     * @param image
     * @param averageRedColor
     * @return matrix of 1 if pixel`s color > average color and 0 otherwise
     */
    public int[][] getAverageMask(BufferedImage image, int averageRedColor) {
        int h = image.getHeight();
        int w = image.getWidth();
        int rgb, red;
        int[][] result = new int[w][h];

        for (int x = 0; x < w; x++)
            for (int y = 0; y < h; y++) {
                rgb = image.getRGB(x, y);
                red = (rgb >> 16) & 0xff;
                if (red < averageRedColor)
                    result[x][y] = 0;
                else result[x][y] = 1;
            }

        return result;
    }

    /**
     * @param mask
     * @return string representation of matrix
     */
    public String averageMaskToHash(int[][] mask) {
        StringBuilder result = new StringBuilder();
        for (int x = 0; x < mask.length; x++)
            for (int y = 0; y < mask.length; y++)
                result.append(mask[x][y]);
        return result.toString();
    }

    public int getIntRgbFromColors(int Red, int Green, int Blue) {
        Red = (Red << 16) & 0x00FF0000;
        Green = (Green << 8) & 0x0000FF00;
        Blue = Blue & 0x000000FF;

        return 0xFF000000 | Red | Green | Blue; //0xFF000000 for 100% Alpha
    }

    /**
     * only for grayscale images
     *
     * @param image
     * @return matrix of pixel`s colors
     */
    public double[][] grayscaleImageToMatrix(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        double[][] result = new double[w][h];

        for (int x = 0; x < w; x++)
            for (int y = 0; y < h; y++)
                result[x][y] = (image.getRGB(x, y) >> 16) & 0xff;
        return result;
    }


}
